package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO 1.查询该用户购物车表中是否有相同的菜品或套餐
     *                        有相同的套餐的话数量+1
     *                        没有的话,判断是套餐还是菜品
     *                        如果是菜品 根据dishid 查询菜品表 给购物车对象赋值
     *                        如果是套餐 根据setmealid 查询菜品表 给购物车对象赋值
     *                        给购物车的其他字段赋值
     *                        插入购物车表数据
     */

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //查询该用户购物车表中是否有相同的菜品或套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() == StatusConstant.ENABLE) {
            //有相同的套餐的话数量+1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //修改菜品/套餐数量
            shoppingCartMapper.updateNumber(shoppingCart);

        } else {
            //如果是菜品 根据dishid 查询菜品表 给购物车对象赋值
            if (shoppingCart.getDishId() != null) {
           Dish dish= dishMapper.selectById(shoppingCart.getDishId());
           shoppingCart.setName(dish.getName());
           shoppingCart.setAmount(dish.getPrice());
           shoppingCart.setImage(dish.getImage());
            }else {
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查询购物车
     */
    @Override
    public List<ShoppingCart>  select() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void delete() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.delete(userId);
    }

}
